//
//  KRStepsCell.m
//  cookBook
//
//  Created by kuroken on 16/1/25.
//  Copyright © 2016年 kuroken. All rights reserved.
//

#import "KRStepsCell.h"
#import <UIImageView+WebCache.h>
#import <PureLayout.h>
#import "KRConst.h"
#import "KRRecipeSteps.h"

// cell重用标识
static NSString * const KRSteps = @"step";

/** 竖直方向padding */
CGFloat const KRStepVerticalPadding = 15;
/** 水平方向padding */
CGFloat const KRStepHorizontalPadding = 15;
CGFloat const KRPhotoImageW = 100;
CGFloat const KRPhotoImageH = 66;
/** introLabel与photoImageView距离 */
CGFloat const KRIntroApartPhoto = 40;

/** 说明的文字大小 */
CGFloat const KRIntroLabelFontSize = 18;

@interface KRStepsCell ()

/** 图片 imageView */
@property (nonatomic, weak) UIImageView *photoImageView;
/** 序号 label */
@property (nonatomic, weak) UILabel *numLabel;
/** 说明 label */
@property (nonatomic, weak) UILabel *introLabel;
/** 图片底部约束 */
@property (nonatomic, strong) NSLayoutConstraint *photoImageViewBottomConstraint;
/** 说明底部约束 */
@property (nonatomic, strong) NSLayoutConstraint *introLabelBottomConstraint;
@end

@implementation KRStepsCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    KRStepsCell *cell = [tableView dequeueReusableCellWithIdentifier:KRSteps];
    if (!cell) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:KRSteps];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // <#初始化代码#>
        
        [self setupSubview];
        [self addConstraint];
    }
    return self;
}

- (void)setStep:(KRRecipeSteps *)step
{
    _step = step;
    
    [self.photoImageView sd_setImageWithURL:[NSURL URLWithString:step.step_photo] placeholderImage:[UIImage imageNamed:@"default_r172-118"]];
    [self.introLabel setText:step.step_intro];
    
    // 计算introLabel的高度
    CGFloat introWeight = KRScreenWidth - KRStepHorizontalPadding * 2 - KRPhotoImageW - KRIntroApartPhoto;
    CGFloat introHeight = [step.step_intro sizeWithFont:[UIFont systemFontOfSize:KRIntroLabelFontSize] maxW:introWeight].height;
    if (KRPhotoImageH < introHeight) { //  图片高度小于说明高度
        if ([self.contentView.constraints containsObject:self.photoImageViewBottomConstraint]){
            [self.photoImageViewBottomConstraint autoRemove];
        }
        if (![self.contentView.constraints containsObject:self.introLabelBottomConstraint]) {
            [self.introLabelBottomConstraint autoInstall];
        }
    } else {
        if ([self.contentView.constraints containsObject:self.introLabelBottomConstraint]) {
            [self.introLabelBottomConstraint autoRemove];
        }
        if (![self.contentView.constraints containsObject:self.photoImageViewBottomConstraint]){
            [self.photoImageViewBottomConstraint autoInstall];
        }
    }
    
}

- (void)setStep_num:(NSInteger)step_num
{
    _step_num = step_num;
    
    [self.numLabel setText:[NSString stringWithFormat:@"%zd", step_num + 1]];
}

/**
 *  初始化子控件
 */
- (void)setupSubview
{
    /** 图片 imageView */
    UIImageView *photoImageView = [[UIImageView alloc] init];
    photoImageView.contentMode = UIViewContentModeScaleAspectFill;
    photoImageView.clipsToBounds = YES;
    [self.contentView addSubview:photoImageView];
    self.photoImageView = photoImageView;
    
    /** 序号 label */
    UILabel *numLabel = [[UILabel alloc] init];
    [numLabel setFont:[UIFont boldSystemFontOfSize:25]];
    [self.contentView addSubview:numLabel];
    self.numLabel = numLabel;
    
    /** 说明 label */
    UILabel *introLabel = [[UILabel alloc] init];
    [introLabel setFont:[UIFont systemFontOfSize:KRIntroLabelFontSize]];
    introLabel.numberOfLines = 0;
    [self.contentView addSubview:introLabel];
    self.introLabel = introLabel;
}

/**
 *  添加约束
 */
- (void)addConstraint
{
    [self.photoImageView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:KRStepVerticalPadding];
    [self.photoImageView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:KRStepHorizontalPadding];
    [self.photoImageView autoSetDimensionsToSize:CGSizeMake(KRPhotoImageW, KRPhotoImageH)];
    [NSLayoutConstraint autoSetPriority:UILayoutPriorityDefaultHigh forConstraints:^{
        self.photoImageViewBottomConstraint = [self.photoImageView autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:KRStepVerticalPadding];
    }];

    [self.numLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.photoImageView withOffset:-5];
    [self.numLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.photoImageView withOffset:10];
    
    [self.introLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.photoImageView];
    [self.introLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.photoImageView withOffset:KRIntroApartPhoto];
    [self.introLabel autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:KRStepHorizontalPadding];
    [NSLayoutConstraint autoSetPriority:UILayoutPriorityDefaultLow forConstraints:^{
        self.introLabelBottomConstraint = [self.introLabel autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:KRStepVerticalPadding];
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
